package com.zy.cat.controller.run.v1;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.common.utils.TJKUtils;
import com.zy.cat.entity.dev.resp.CatDevRemitRecordResp;
import com.zy.cat.entity.game.CardDrawHistory;
import com.zy.cat.entity.game.resp.CardDrawHistoryResp;
import com.zy.cat.entity.resp.Pages;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.entity.resp.ResultPage;
import com.zy.cat.entity.run.DailyStepCoinsHistory;
import com.zy.cat.entity.run.repository.DailyStepCoinsHistoryRepository;
import com.zy.cat.entity.run.resp.DailyStepInfoResp;
import com.zy.cat.entity.wx.WxSessionInfo;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.CatRunService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @info
 * @author Link
 * @date 2019-11-12 16:31
 */
@Api(value = "cat_run_step", tags = { "9趣猫运动赚钱开放API" }, description = "9趣猫运动赚钱开放接口")
@RestController
@RequestMapping("/v1/run")
@Slf4j
public class CatRunController {

	@Autowired
	CacheService cacheService;

	@Autowired
	CatRunService catRunService;

	@Autowired
	DailyStepCoinsHistoryRepository dailyStepConvertHistoryRepository;

	@PostMapping("/step/info")
	@ApiOperation(position = 1, value = "获取运动信息", notes = "运动步数、三个随机金币数", response = DailyStepInfoResp.class, responseContainer = "List", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "登录返回字段", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_rtype_pid_utoken_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_rtype_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object getStepInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<DailyStepInfoResp>> result = new Callable<RSPE<DailyStepInfoResp>>() {
			@Override
			public RSPE<DailyStepInfoResp> call() throws Exception {
				return new RSPE<DailyStepInfoResp>(catRunService.getStepInfo(pid));
			}
		};
		return result;
	}

	@PostMapping("/step/getcoin")
	@ApiOperation(position = 2, value = "领取运动金币", notes = "领取四个运动金币", response = RSPE.class, responseContainer = "List", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "fid", value = "领取金币ID", example = "", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "ftype", value = "是否翻倍（0.不翻倍、1.翻倍）", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "登录返回字段", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_rtype_pid_utoken_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_rtype_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object getStepConvert(Integer rtype, String fid, Integer ftype, String pid, String utoken, String uuid,
			String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (StringUtils.isBlank(fid) || fid.length() != 32) {
			return new RSPE<Object>(-3, "金币ID错误！");
		}
		if (ftype == null || ftype < 0 || ftype > 1) {
			return new RSPE<Object>(-4, "是否翻倍错误！");
		}
		DailyStepCoinsHistory step = dailyStepConvertHistoryRepository.findByPidAndFid(pid, fid);

		/* 判断金币是否存在 */
		if (step == null) {
			return new RSPE<Object>(-5, "领取的金币不存在！");
		}
		/* 判断金币是否已领取 */
		if (step.getDstatus() == 1 && ftype == 0) {
			return new RSPE<Object>(-6, "金币已经领取！");
		}
		/* 判断金币是否已经翻倍 */
		if (ftype == 1) {
			if (step.getFstatus() == 1) {
				return new RSPE<Object>(-7, "金币已经翻倍！");
			}
			if (step.getCtype() == 0) {
				return new RSPE<Object>(-8, "兑换金币不支持翻倍！");
			}
		}
		if (step.getCtype() != 0 && !LocalDateTime.now().isAfter(TJKUtils.parse2LocalDateTime(step.getEtime()))) {
			return new RSPE<Object>(-9, "未到领取时间！");
		}
		Callable<RSPE<Boolean>> result = new Callable<RSPE<Boolean>>() {
			@Override
			public RSPE<Boolean> call() throws Exception {
				if (catRunService.getStepConvert(rtype, pid, ftype, step)) {
					return new RSPE<Boolean>(1, "领取成功！");
				}
				return new RSPE<Boolean>(0, "领取失败！");
			}
		};
		return result;
	}

	@PostMapping("/step/speed")
	@ApiOperation(position = 3, value = "运动金币加速", notes = "金币加速3分钟", response = RSPE.class, responseContainer = "List", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "sid", value = "加速ID", example = "", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "登录返回字段", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_rtype_pid_utoken_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_rtype_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object setStepSpeed(Integer rtype, String sid, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (StringUtils.isBlank(sid) || sid.length() != 32) {
			return new RSPE<Object>(-3, "加速ID错误！");
		}
		List<DailyStepCoinsHistory> steps = dailyStepConvertHistoryRepository.getStepSpeedInfo(pid, sid);
		/* 判断金币是否存在 */
		if (steps == null || steps.isEmpty()) {
			return new RSPE<Object>(-5, "加速金币不存在！");
		}
		/* 判断金币是否已领取 */
		if (steps.get(0).getJstatus() == 1) {
			return new RSPE<Object>(-6, "金币已经加速！");
		}

		Callable<RSPE<Boolean>> result = new Callable<RSPE<Boolean>>() {
			@Override
			public RSPE<Boolean> call() throws Exception {
				if (catRunService.setStepSpeed(rtype, pid, sid,steps)) {
					return new RSPE<Boolean>(1, "加速成功！");
				}
				return new RSPE<Boolean>(0, "加速失败！");
			}
		};
		return result;
	}
	
	@PostMapping("/step/data")
	@ApiOperation(value = "9趣猫微信小程序步数信息", position = 4, notes = "9趣猫微信小程序步数信息", consumes = "application/x-www-form-urlencoded", response = RSPE.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "code", value = "微信code", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "encryptedData", value = "微信encryptedData", example = "", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "iv", value = "微信iv", example = "", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_encryptedData_code_iv_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_encryptedData_code_iv", MD5 = "token")
	public Object getRunStepData(String code,String encryptedData,String iv, String uuid,String token) {
		
		Callable<RSPE<Integer>> result = new Callable<RSPE<Integer>>() {
			@Override
			public RSPE<Integer> call() throws Exception {
				Integer step = catRunService.getRunStepData(code,encryptedData,iv);
				if(step==null) {
					return new RSPE<Integer>(0, "获取失败！");
				}
				return new RSPE<Integer>(step);
			}
		};
		return result;
	}
	
	@PostMapping("/step/sync")
	@ApiOperation(value = "9趣猫同步用户步数信息", position = 4, notes = "每当用户打开走路赚钱页面进行同步一次,以天为统计单位", consumes = "application/x-www-form-urlencoded", response = RSPE.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "登录返回字段", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "data", value = "步数信息(使用AES加密)", example = "uabT3qeP88E514E3TCpVLg", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_rtype_data_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_pid_rtype_data_utoken", MD5 = "token")
	public Object syncRunStep(Integer rtype,String pid,String data, String utoken,String uuid,String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<Integer>> result = new Callable<RSPE<Integer>>() {
			@Override
			public RSPE<Integer> call() throws Exception {
				Integer resultStep = catRunService.syncRunStep(rtype,pid,data,utoken);
				if(resultStep==null) {
					return new RSPE<Integer>(0, "同步失败！");
				}
				return new RSPE<Integer>(1,"同步成功！");
			}
		};
		return result;
	}
}
